package com.cardone.platform.common.support;

import java.util.List;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import org.apache.commons.lang3.BooleanUtils;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.support.TaskUtils;

import com.cardone.platform.authority.dto.NavigationDto;
import com.cardone.platform.authority.service.AuthorityService;
import com.cardone.platform.configuration.dto.DictionaryTypeDto;
import com.cardone.platform.configuration.dto.SiteDto;
import com.cardone.platform.configuration.service.ConfigurationService;

/**
 * 初始化数据支持
 * 
 * @author yaoht
 */
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class InitDataSupport {
	/**
	 * 权限服务
	 */
	private AuthorityService authorityService;
	/**
	 * 配置服务
	 */
	private ConfigurationService configurationService;

	/**
	 * 配置集合
	 */
	private List<DictionaryTypeDto> dictionaryTypeList;

	/**
	 * 是否初始化数据
	 */
	private Boolean initData;

	/**
	 * 导航集合
	 */
	private List<NavigationDto> navigationList;

	/**
	 * 站点集合
	 */
	private List<SiteDto> siteList;

	/**
	 * 异步调用
	 */
	private TaskExecutor taskExecutor;

	/**
	 * 初始化
	 */
	public void init() {
		if (BooleanUtils.isNotTrue(this.initData)) {
			return;
		}

		this.taskExecutor.execute(TaskUtils.decorateTaskWithErrorHandler(new Runnable() {
			@Override
			public void run() {
				InitDataSupport.this.configurationService.getDictionaryService()
				        .initDictionary(InitDataSupport.this.dictionaryTypeList);

				InitDataSupport.this.configurationService.getSiteService().initSite(InitDataSupport.this.siteList);

				InitDataSupport.this.authorityService.getNavigationService().initNavigation(InitDataSupport.this.navigationList);
			}
		}, null, true));
	}
}
